package com.example.linyiquan.fuck;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by linyiquan on 17-5-4.
 */

public class FuckTime {

    private ScheduledExecutorService ses= Executors.newScheduledThreadPool(10000);
    private String url="http://192.168.1.101:8080/transportservice/type/jason/action/GetLightSenseValve.do";

    public FuckTime(){}

    public void execute(){

        Runnable task=new Runnable() {
            @Override
            public void run() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String response=FuckTime.net(url,null,"POST");
                            System.out.println("");
                            System.out.println("-------------this line power by fuck thinkthk---------------------");
                            System.out.println(response);
                            System.out.println("---------------------------------------------------------");
                            System.out.println("");
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        };
        if (ses.isShutdown()){
            this.ses=Executors.newScheduledThreadPool(1);
            this.ses.scheduleAtFixedRate(task,0L,1L, TimeUnit.MICROSECONDS);
        }else {
            this.ses.scheduleAtFixedRate(task,0L,1L,TimeUnit.MICROSECONDS);
        }
    }

    public void stop(){
        this.ses.shutdown();
    }

    public static String net(String strUrl,Map params,String method)throws Exception{
        HttpURLConnection conn=null;
        BufferedReader reader=null;
        String rs=null;

        try {
            StringBuffer sb=new StringBuffer();
            if (method==null||method.equals("GET")){
                strUrl=strUrl+"?"+urlencode(params);
            }

            URL url=new URL(strUrl);
            conn= (HttpURLConnection) url.openConnection();
            if (method!=null&&!method.equals("GET")){
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
            }else {
                conn.setRequestMethod("GET");
            }

            conn.setRequestProperty("User-agent","Mozilla/5.0 (Linux; Android 4.2.1; Nexus 7 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19");
            conn.setUseCaches(false);
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(false);
            conn.connect();
            if (params!=null&&method.equals("POST")){
                try{
                    DataOutputStream out=new DataOutputStream(conn.getOutputStream());
                    out.writeBytes(urlencode(params));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            InputStream is=conn.getInputStream();
            reader=new BufferedReader(new InputStreamReader(is,"UTF-8"));
            String strRead=null;

            while ((strRead=reader.readLine())!=null){
                sb.append(strRead);
            }
            rs=sb.toString();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (reader!=null){
                reader.close();
            }

            if (conn!=null){
                conn.disconnect();
            }
        }

        return rs;
    }

    public static String urlencode(Map<String,Object>data){
        StringBuilder sb=new StringBuilder();
        Iterator var2=data.entrySet().iterator();

        while (var2.hasNext()){
            Map.Entry i= (Map.Entry) var2.next();
            try {
                sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
            }catch (UnsupportedEncodingException var5){
                var5.printStackTrace();
            }
        }
        return sb.toString();
    }


}
